/*
    游戏通用特效
 */
class EffectUtil {
    private static _instance:EffectUtil;

    public static instance(){
        if(!this._instance)
        {
            this._instance = new EffectUtil();
        }
        return this._instance;
    }

    /**
    * obj           对象
    */    
    public StopEffect(obj):void{
        egret.Tween.removeTweens(obj);
    }

    /**
     * 打开弹窗动画
     * 0：没有动画 1:从中间轻微弹出 2：从中间猛烈弹出  3：从左向右 4：从右向左 5、从上到下 6、从下到上
     */
    public OpenUIEffect(panel:any, effectType:number, isAlert:boolean = false)
    {
         //以下是弹窗动画
        let popUpWidth = panel.width;
        let popUpHeight = panel.height;
        let originalWidth = 1280;
        let originalHeight = 720;
        
        var leftX:number = originalWidth/2 - popUpWidth/2;
        var upY:number = originalHeight/2 - popUpHeight/2;

        switch(effectType){
            case 0:
                break;
            case 1:
                panel.alpha = 0;
                panel.scaleX = 0.5;
                panel.scaleY = 0.5;
                panel.x = panel.x + popUpWidth/4;
                panel.y = panel.y + popUpHeight/4;
                egret.Tween.get(panel).to({alpha:1,scaleX:1,scaleY:1,x:panel.x - popUpWidth/4,y:panel.y - popUpHeight/4},300,egret.Ease.backOut); 
                break;
            case 2:
                panel.alpha = 1;
                panel.scaleX = 0.5;
                panel.scaleY = 0.5;
                panel.x = panel.x + popUpWidth/4;
                panel.y = panel.y + popUpHeight/4;
                egret.Tween.get(panel).to({alpha:1,scaleX:1,scaleY:1,x:panel.x - popUpWidth/4,y:panel.y - popUpHeight/4},600,egret.Ease.elasticOut); 
                break;
            case 3:
                if(isAlert){
                    panel.x = - popUpWidth;
                    egret.Tween.get(panel).to({x:leftX},500,egret.Ease.cubicOut); 
                }else{       
                    panel.x = - popUpWidth;
                    egret.Tween.get(panel).to({x:0},500,egret.Ease.cubicOut);
                }
                break;
            case 4:
                if(isAlert){
                    panel.x = popUpWidth;
                    egret.Tween.get(panel).to({x:leftX},500,egret.Ease.cubicOut);  
                }else{
                    panel.x = popUpWidth;
                    egret.Tween.get(panel).to({x:0},500,egret.Ease.cubicOut);  
                }
                break;
            case 5:
                if(isAlert){
                    panel.y = - popUpHeight;
                    egret.Tween.get(panel).to({y:upY},500,egret.Ease.cubicOut); 
                }else{
                    panel.y = - popUpHeight;
                    egret.Tween.get(panel).to({y:0},500,egret.Ease.cubicOut);   
                }
                break;
            case 6:
                if(isAlert){
                    panel.y = originalHeight;
                    egret.Tween.get(panel).to({y:upY},500,egret.Ease.cubicOut); 
                }else{
                    panel.y = popUpHeight;
                    egret.Tween.get(panel).to({y:0},500,egret.Ease.cubicOut); 
                }
                break;
            default:
                break;
        }		
    }

        //左右摇摆
    public rockEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                obj.rotation = -10;
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({
                    rotation:-10
                },2000,egret.Ease.sineInOut).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({
                    rotation:10
                },2000,egret.Ease.sineInOut)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    /**
     * 关闭界面动画
     */
    public CloseUIEffect(panel:any, effectType:number, call:Function)
    {
         //以下是弹窗动画

        switch(effectType){
            case 0:
                break;
            case 1:
                egret.Tween.get(panel).to({alpha:0,scaleX:0,scaleY:0,x:panel.x + panel.width/2,y:panel.y + panel.height/2},300).call(call); 
                break;
            case 2:
		        egret.Tween.get(panel).to({x:panel.width},500,egret.Ease.cubicOut).call(call); 
                break;
            case 3:
		        egret.Tween.get(panel).to({x:-panel.width},500,egret.Ease.cubicOut).call(call);        
                break;
            case 4:
		        egret.Tween.get(panel).to({y:panel.height},500,egret.Ease.cubicOut).call(call);             
                break;
            case 5:
		        egret.Tween.get(panel).to({y:-panel.height},500,egret.Ease.cubicOut).call(call);              
                break;
            default:
                break;
        } 		
    }

    /**
	 * 数字变化
	 * t Label或BitmapLabel
	 */
	public NumChange(t:any, begin:number, end:number, time:number = 500, tittle:string='',endTittle:string=''){
		if(begin == end){
			return;
		}
		var o = {n:begin};
		t.text = begin.toString();
		egret.Tween.get(o, {
			onChange:function (){
				t.text = tittle+Math.round(o.n).toString()+endTittle;
			}
		}, this).to({n:end}, time);
	}

     /**
    * 按钮特效
    * obj           对象
    */
    public playBtnEffect(obj):void{
        egret.Tween.get(obj).to({ scaleX: 0.6, scaleY: 0.6}, 100).call(()=>{
            egret.Tween.get(obj).to({ scaleX: 1, scaleY: 1}, 100).call(()=>{// egret.Ease.elasticOut
                if(obj.isPlayingAnim)
                    obj.isPlayingAnim = false;
            });
        });
    }

    //对象旋转特效
    //obj   旋转对象
    //time  旋转一周用时，毫秒
    public rotationEffect(obj,time:number = 1000):void{
        var onComplete1:Function = function(){
            if(obj != null){
                obj.rotation = 0;
                egret.Tween.get(obj).to({rotation:360},time).call(onComplete1,this);   
            }
        };
        obj.rotation = 0;
        egret.Tween.get(obj).to({rotation:360},time).call(onComplete1,this);   
    }

        /**
    * 显示对象上下浮动特效
    * obj           对象
    * time          浮动时间 毫秒
    * space         浮动高度
    * todo          多个对象跳动
    */    
    public flyObj(obj,time,space:number = 50):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({y:obj.y - space},time).call(onComplete1,this);
                };  
                egret.Tween.get(obj).to({y:obj.y + space},time).call(onComplete2,this);
            }
        };  
        onComplete1();
    }

    /**
    * 显示对象放大缩小
    * obj           对象
    */    
    public ScaleEffect(obj, scale = 1.3):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({scaleX:1, scaleY:1},500).call(onComplete1,this);
                };  
                egret.Tween.get(obj).to({scaleX:scale, scaleY:scale},500).call(onComplete2,this);
            }
        };  
        onComplete1();
    }

        /**
    * 文字打字机效果
    * obj           文本对象
    * content       文字
    * interval      打字间隔 毫秒
    */    
    public typerEffect(obj,content:string = "",interval:number = 200):void{
        var strArr:Array<any> = content.split("");
        var len:number = strArr.length;
        for (var i = 0; i < len; i++){
            egret.setTimeout(function () {              
                obj.appendText(strArr[Number(this)]);
            }, i, interval*i);              
        }
    }

    //对象上下左右來回动
    public MoveEffect(obj):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({x:obj.x -30, y:obj.y - 30},500).call(onComplete1,this);   
                }
                egret.Tween.get(obj).to({x:obj.x +30, y:obj.y + 30},500).call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //对象左右來回动
    public MoveEffect2(obj):void{
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({x:obj.x - 300},500).call(onComplete1,this);   
                }
                egret.Tween.get(obj).to({x:obj.x + 300},500).call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //屏幕抖动的特效
    public ShakeEffect(obj, call:Function, scope):void{
        var tw = egret.Tween.get(obj);
        tw.to( {x:-10}, 50)
        .to( {x:0}, 50)
        .to( {x:-10}, 50)
    .to( {x:0}, 50).wait(200).call(()=>{
        if(call)
        {
            call.call(scope);
        }
        })
    }

    //向内再向外移出
    /**
     * dir = -1, 先向左再向右移除
     * dir = 1, 先向右再向左移除
     */
    public RightOrLeftRmove(obj, dir, width):void{
        let w = dir == 1 ? obj.width : 0;
        if(obj != null){
            egret.Tween.get(obj).to({x:obj.x + dir * 10},50, egret.Ease.sineInOut).wait(50)
            .to({
                x:obj.x - dir * (width - obj.x - w),
                alpha : 0
            }, 400, egret.Ease.sineIn);  
        }
    }

    //渐渐消失
    public Disappear(obj, time)
    {
        if(obj != null){
            egret.Tween.get(obj).to({alpha:0}, time, egret.Ease.sineInOut);
        }
    }

    /**
	 * 321倒计时
	 */
	public countDown(call?:Function, scope?){
        let width = GlobalManager.instance().curWidth();
        let height = GlobalManager.instance().curHeight();
        let addBlackBg = function()
        {
            let rectBg = new eui.Rect(width, height, 0x000000);
            rectBg.alpha = 0.3;
            rectBg.visible = true;
            rectBg.horizontalCenter = rectBg.verticalCenter = 0;
            // obj.addChild(rectBg);
            return rectBg;
        }

		let lb_num:eui.Label = new eui.Label();
        let count:number = 4;
        lb_num.text = count.toString();
        lb_num.size = 180;
        // lb_num.style = "font_style_1";
        lb_num.horizontalCenter = lb_num.verticalCenter = 0;

        // let Layer:core.BaseLayer = core.LayerManager.instance().TopViewLayer;
        // addBlackBg(Layer);
        // Layer.addChild(lb_num);
        GlobalManager.instance().addChildToLayer(addBlackBg(), LayerType.Tips);
        GlobalManager.instance().addChildToLayer(lb_num, LayerType.Tips);

        let onComplete1:Function = function(){
            let onComplete2:Function = function(){
                egret.Tween.get(lb_num).wait(250).call(onComplete1,this);
            }
            egret.Tween.get(lb_num).call(()=>{
                count --;
                if(count >= 0){
                    lb_num.text = count.toString();
                }
            })
            .to({scaleY:2,scaleX:2},250)
            .to({scaleY:1,scaleX:1},250).call(()=>{
                if(count == 0)
                {
                    egret.Tween.get(lb_num).wait(500).call(()=>{
                        lb_num.text = "开 始";
                    }).wait(500).call(()=>{
                        egret.Tween.removeTweens(lb_num);
                        GlobalManager.instance().getLayer(LayerType.Tips).removeChildren();
                        if(call)
                        {
                            call.call(scope);
                        }
                    });
                }
                else
                {
                    egret.Tween.get(lb_num).call(onComplete2,this);
                }
            });
        };  
        onComplete1();
	}

        //抖动特效
    public shakeEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).wait(1000).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({rotation:30},50).
                to({rotation:-30},100)
                .to({rotation:30},100)
                .to({rotation:-30},100)
                .to({rotation:30},100)
                .to({rotation:0},50)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //上下动特效
    public shakeEffect1(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).wait(1000).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({y:obj.y - 30, scaleX:0.8},50).
                to({y:obj.y, scaleX:1.3},100)
                .to({y:obj.y - 20, scaleX:0.85},80)
                .to({y:obj.y, scaleX:1.2},100)
                .to({y:obj.y - 10, scaleX:0.9},80)
                .to({y:obj.y, scaleX:1},50)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

     //放大透明度变0再重复
    public scaleAlphaEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    obj.scaleX = obj.scaleY = obj.alpha = 1;
                    egret.Tween.get(obj).wait(1000).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({
                    scaleX:1.8,
                    scaleY:1.8,
                    alpha:0
                },500)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //透明度特效
    public AlphaEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({
                    alpha:1,
                },1500).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({
                    alpha:0,
                },1500)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

     //放大缩小再重复
    public scaleEffect(obj)
    {
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({
                        scaleX:1,
                        scaleY:1,
                    },500).call(onComplete1,this);
                }
                egret.Tween.get(obj).to({
                    scaleX:1.2,
                    scaleY:1.2,
                },500)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }

    //按照配置度数执行重复动画
    public excuteAnimation(data:string, obj, time, springback, isJump = false)
    {
        if(!obj || !data) return;
        let rorationArr = data.split(',');
        let effectType = springback == 0 ? egret.Ease.sineInOut : egret.Ease.backInOut;
        let curCustoms = GameManager.instance.curCustoms;
        let curIndex = GameManager.instance.curCustomsIndex;
        if(curCustoms > 80 && curIndex == 4)
        {
            effectType = egret.Ease.backInOut
        }
        let index = 0;
        let dir = 1;
        let oriX = obj.x;
        let count = 0;
        var onComplete2:Function = function(){
            index = 0;
            if(isJump)
            {
                let Y = 80;
                
                egret.Tween.get(obj).to({
                    x:obj.x + dir * 25,
                    y:obj.y -  Y
                },200).to({
                    x:obj.x + dir * 50,
                    y:obj.y
                },200).to({
                    x:obj.x + dir * 100,
                    y:obj.y -  Y
                },200).to({
                    x:obj.x + dir * 125,
                    y:obj.y
                },200).to({
                    x:obj.x + dir * 150,
                    y:obj.y -  Y
                },200).to({
                    x:obj.x + dir * 175,
                    y:obj.y
                },200).
                call(onComplete1, this)
                if(count == 2 || count == 0)
                {
                    dir *= -1;
                    count = 0;
                }
                count++;
                return;
            }
            onComplete1();
        };
        
        if(!time) time = 1500;
        var onComplete1:Function = function(){
            let roration = rorationArr[index++];
            let angle = parseInt(roration); 
            if(roration){
                egret.Tween.get(obj).to({
                    rotation:obj.rotation + angle
                },time, effectType).wait(100)
                .call(()=>{
                    if(rorationArr[index])
                    {
                        onComplete1.call(this);
                    }
                    else
                    {
                        onComplete2.call(this);
                    }
                });  
            }
        };
        onComplete1();
    }

    //左右旋转动画
    public LREffect(obj)
    {
        if(!obj)return;
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).to({
                        rotation:45
                    },500).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({
                    rotation:-45
                },500)
                .call(onComplete2,this);  
            }
        };
        egret.Tween.get(obj).to({
            rotation:45
        },250).call(onComplete1,this)
    }

    /**
    * 弹跳缩放
    * obj           对象
    */ 
    public BallEffect(obj):void{
        let frame = 1000.00 / 24
        var onComplete1:Function = function(){
            if(obj != null){
                var onComplete2:Function = function(){
                    egret.Tween.get(obj).wait(5 * frame).call(onComplete1,this);
                };  
            egret.Tween.get(obj).to({scaleY:0.9},6 * frame).to({scaleY:1.06},6 * frame)
                .to({scaleY:0.94},3 * frame).to({scaleY:1},3 * frame).call(onComplete2,this);
            }
        };  
        onComplete1();
    }

    /**
     * 抖屏特效
     */
    public shakeScreen(obj, dis = 20):void{
        if(!obj)return;
        var onComplete1:Function = function(){
            if(obj != null){
                let dir = Math.random() > 0.5 ? 1 : -1;
                var onComplete2:Function = function(){
                    dir *= -1;
                    egret.Tween.get(obj).to({
                        x : obj.x -dis,
                        y : obj.y -dis
                    },50).call(onComplete1,this);  
                }
                egret.Tween.get(obj).to({
                    x : obj.x + dis,
                    y : obj.y + dis
                },50)
                .call(onComplete2,this);  
            }
        };
        onComplete1();
    }
}